package com.xyy.saas.payment.merchant.core.service;

import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.BusinessTypeEnum;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.cores.param.BankQueryParam;
import com.xyy.saas.payment.cores.param.Pair;
import com.xyy.saas.payment.cores.param.SubBankQueryParam;
import com.xyy.saas.payment.dao.model.BaseBankBranchInfo;
import com.xyy.saas.payment.dao.model.BaseBankInfo;
import com.xyy.saas.payment.dao.model.BaseBankPinganInfo;
import com.xyy.saas.payment.service.BaseBankBranchService;
import com.xyy.saas.payment.service.BaseBankPinganService;
import com.xyy.saas.payment.service.BaseBankService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BaseBankService2 {
    @Resource
    private BaseBankPinganService baseBankPinganService;
    @Resource
    private BaseBankService baseBankService;
    @Resource
    private BaseBankBranchService baseBankBranchService;

    /**
     * 查询银行
     */
    public List<Pair> getBank(BankQueryParam param) throws PaymentException {
        if(StringUtils.isBlank(param.getBusinessIdType())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "业务线不能为空");
        }

        List<Pair> result = new ArrayList<>();

        if(param.getBusinessIdType().contains(BusinessTypeEnum.EC.getType())) {
            List<BaseBankPinganInfo> items = baseBankPinganService.queryByBankName(param.getBankName());

            items.forEach(item -> {
                result.add(Pair.build(item.getBankShortName(), item.getDefaultBranchCode()));
            });
        }
        else {
            List<BaseBankInfo> items = baseBankService.queryByBankName(param.getBankName());

            items.forEach(item -> {
                result.add(Pair.build(item.getBankName(), item.getBankCode()));
            });
        }

        return result;
    }

    /**
     * 查询支行
     */
    public List<Pair> getBranchBank(SubBankQueryParam param) throws PaymentException {
        if(StringUtils.isBlank(param.getBusinessIdType())) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "业务线不能为空");
        }

        List<Pair> result = new ArrayList<>();

        if(param.getBusinessIdType().contains(BusinessTypeEnum.EC.getType())) {
            List<BaseBankBranchInfo> items = baseBankBranchService.queryByName(param.getBankName(), param.getSubBankName());

            for(BaseBankBranchInfo item : items) {
                result.add(Pair.build(item.getBankBranchName(), item.getBankBranchCode()));
            }
        }
        else {
            List<BaseBankBranchInfo> items = baseBankBranchService.queryByName(param.getBankName(), param.getSubBankName());

            for(BaseBankBranchInfo item : items) {
                result.add(Pair.build(item.getBankBranchName(), item.getBankBranchCode()));
            }
        }

        return result;
    }

    /**
     * 根据简称查名称
     */
    public String convertPinganShortBankName2BankName(String shortName) throws PaymentException {
        BaseBankPinganInfo baseBankPinganInfo = baseBankPinganService.queryByBankName2(shortName);

        if(baseBankPinganInfo == null) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "根据银行简称未查询到全称, 简称=" + shortName);
        }

        return baseBankPinganInfo.getBankName();
    }
}
